package com.mate.cloud.user.enums;

import com.google.common.collect.Lists;
import com.mate.cloud.enums.CommonEnum;
import com.mate.cloud.enums.CommonEnumVO;
import com.mate.cloud.enums.mvc.CommonEnumRegistry;
import com.mate.cloud.user.domain.User;
import com.mate.cloud.user.service.UserService;
import com.mate.cloud.web.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author cong zhi
 * @description
 * @email 448341911@qq.com
 * @date $ $
 * @version: v1.0
 */
@RestController
@RequestMapping("enum")
public class EnumController {

    @Resource
    private UserService userService;
    @Resource
    private CommonEnumRegistry commonEnumRegistry;

    @GetMapping("/paramToEnum")
    public RestResult<CommonEnumVO> paramToEnum(@RequestParam("genderEnum") GenderEnum genderEnum) {
        return RestResult.success(CommonEnumVO.from(genderEnum));
    }

    @GetMapping("/types")
    public RestResult<List<String>> enumTypes() {
        Map<String, List<CommonEnum>> dict = this.commonEnumRegistry.getNameDict();


        return RestResult.success(Lists.newArrayList(dict.keySet()));

    }

    @GetMapping("/list")
    public RestResult<List<User>> users() {
        List<User> users = this.userService.list();

        return RestResult.success(users);
    }

    @PostMapping("/add")
    public String hello(@RequestBody User user) {

        boolean result = userService.save(user);
        if (result) {
            return "添加成功！";
        } else {
            return "添加失败!";
        }
    }

}
